import Taro from '@tarojs/taro';

// 该文件重构为只处理本地存储的mock请求，不再进行实际网络请求

export const request = async <T = any>({
  url,
  method = 'GET',
  data = {},
  headers = {}
}: {
  url: string;
  method?: 'GET' | 'POST' | 'PUT' | 'DELETE';
  data?: any;
  headers?: Record<string, string>;
}): Promise<T> => {
  console.log('Mock请求:', { url, method, data, headers });
  
  // 检查登录状态 (适用于需要认证的请求)
  if (url !== '/user/login' && url !== '/user/register') {
    const token = Taro.getStorageSync('token');
    if (!token) {
      Taro.navigateTo({ url: '/pages/login/index' });
      throw new Error('未登录或登录已过期');
    }
  }
  
  // 这里可以根据不同的URL返回不同的mock数据
  // 但由于我们已经在服务层处理了模拟数据，这里只是保留接口兼容性
  
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({} as T);
    }, 300);
  });
};

// 保留API接口，但实际不使用
export const get = <T = any>(url: string, data = {}, headers = {}) => {
  return request<T>({ url, method: 'GET', data, headers });
};

export const post = <T = any>(url: string, data = {}, headers = {}) => {
  return request<T>({ url, method: 'POST', data, headers });
};

export const put = <T = any>(url: string, data = {}, headers = {}) => {
  return request<T>({ url, method: 'PUT', data, headers });
};

export const del = <T = any>(url: string, data = {}, headers = {}) => {
  return request<T>({ url, method: 'DELETE', data, headers });
}; 